package com.nyist.threadCase;

import com.google.common.util.concurrent.AtomicDouble;
import lombok.extern.slf4j.Slf4j;

import java.util.Random;

/**
 * 1.计算一亿个随机数的和
 *
 * @ClassName MyThreadRunnable
 * @Description
 * @Author xiaoyang
 * @Date 2023/04/03/20:19
 */
@Slf4j
public class MyThreadSum {

    private static final double[] nums;

    private static final Random rand = new Random();
    private static double result = 0.0, result1 = 0.0, result2 = 0.0, result3 = 0.0;

    private static volatile AtomicDouble sum = new AtomicDouble(0.0);

    static {
        int randNum = 1_0000_0000;
        nums = new double[randNum];
        for (int i = 0; i < nums.length; i++) {
            nums[i] = rand.nextDouble();
        }
    }

    public static void main(String[] args) throws InterruptedException {
        MyThreadSum.signalSum();
        MyThreadSum.prototypeSum();
        MyThreadSum.prototypeVolatileSum();
        MyThreadSum.prototypeSumTwo();
    }

    private static void signalSum() {
        result = 0.0;
        long startTime = System.currentTimeMillis();
        for (double num : nums) {
            result += num;
            // log.info("=====>>>ThreadName:{} sum={}", NyistThreadUtils.getThreadName(), result);
        }
        log.info("======>>>单线程,求和,结果为:{}", result);
        log.info("======>>>单线程,求和,耗时为:{}", System.currentTimeMillis() - startTime);
    }

    private static void prototypeVolatileSum() throws InterruptedException {
        Thread t1 = new Thread(() -> {
            for (int i = 0; i < nums.length / 4; i++) {
                sum.addAndGet(nums[i]);
                // log.info("=====>>>ThreadName:{} sum={}", NyistThreadUtils.getThreadName(), result1);
            }
        }, "线程一");
        Thread t2 = new Thread(() -> {
            for (int i = nums.length / 4; i < (nums.length - nums.length / 4) / 2; i++) {
                sum.addAndGet(nums[i]);
                // log.info("=====>>>ThreadName:{} sum={}", NyistThreadUtils.getThreadName(), result2);
            }
        }, "线程二");
        Thread t3 = new Thread(() -> {
            for (int i = (nums.length - nums.length / 4) / 2; i < (nums.length); i++) {
                sum.addAndGet(nums[i]);
                // log.info("=====>>>ThreadName:{} sum={}", NyistThreadUtils.getThreadName(), result2);
            }
        }, "线程三");

        long startTime = System.currentTimeMillis();

        t1.start();
        t2.start();
        t3.start();

        t1.join();
        t2.join();
        t3.join();

        log.info("======>>>多线程2222,求和,结果为:{}", sum);
        log.info("======>>>多线程2222,求和,耗时为:{}", System.currentTimeMillis() - startTime);
    }

    private static void prototypeSum() throws InterruptedException {

        Thread t1 = new Thread(() -> {
            for (int i = 0; i < nums.length / 4; i++) {
                result1 += nums[i];
                // log.info("=====>>>ThreadName:{} sum={}", NyistThreadUtils.getThreadName(), result1);
            }
        }, "线程一");
        Thread t2 = new Thread(() -> {
            for (int i = nums.length / 4; i < (nums.length - nums.length / 4) / 2; i++) {
                result2 += nums[i];
                // log.info("=====>>>ThreadName:{} sum={}", NyistThreadUtils.getThreadName(), result2);
            }
        }, "线程二");
        Thread t3 = new Thread(() -> {
            for (int i = (nums.length - nums.length / 4) / 2; i < (nums.length); i++) {
                result3 += nums[i];
                // log.info("=====>>>ThreadName:{} sum={}", NyistThreadUtils.getThreadName(), result2);
            }
        }, "线程三");

        long startTime = System.currentTimeMillis();

        t1.start();
        t2.start();
        t3.start();

        t1.join();
        t2.join();
        t3.join();

        log.info("======>>>多线程,三,求和,结果为:{}", result1 + result2 + result3);
        log.info("======>>>多线程,三,求和,耗时为:{}", System.currentTimeMillis() - startTime);
    }

    private static void prototypeSumTwo() throws InterruptedException {
        result1 = 0.0;
        result2 = 0.0;
        Thread t1 = new Thread(() -> {
            for (int i = 0; i < nums.length / 2; i++) {
                result1 += nums[i];
                // log.info("=====>>>ThreadName:{} sum={}", NyistThreadUtils.getThreadName(), result1);
            }
        }, "线程一");
        Thread t2 = new Thread(() -> {
            for (int i = nums.length / 2; i < (nums.length); i++) {
                result2 += nums[i];
                // log.info("=====>>>ThreadName:{} sum={}", NyistThreadUtils.getThreadName(), result2);
            }
        }, "线程二");

        long startTime = System.currentTimeMillis();

        t1.start();
        t2.start();

        t1.join();
        t2.join();

        log.info("======>>>多线程,双,求和,结果为:{}", result1 + result2);
        log.info("======>>>多线程,双,求和,耗时为:{}", System.currentTimeMillis() - startTime);
    }

}